package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.enums.BusinessType;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.ApCookbookInstruction;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.IApCookbookInstructionService;
import com.mars.module.admin.request.ApCookbookInstructionRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 菜谱步骤控制层
 *
 * @author mars
 * @date 2024-02-24
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "菜谱步骤接口管理",tags = "菜谱步骤接口管理")
@RequestMapping("/admin/apCookbookInstruction" )
public class ApCookbookInstructionController {

    private final IApCookbookInstructionService iApCookbookInstructionService;

    /**
     * 分页查询菜谱步骤列表
     */
    @ApiOperation(value = "分页查询菜谱步骤列表")
    @PostMapping("/pageList")
    public R<PageInfo<ApCookbookInstruction>> pageList(@RequestBody ApCookbookInstructionRequest apCookbookInstructionRequest) {
        return R.success(iApCookbookInstructionService.pageList(apCookbookInstructionRequest));
    }

    /**
     * 获取菜谱步骤详细信息
     */
    @ApiOperation(value = "获取菜谱步骤详细信息")
    @GetMapping(value = "/query/{id}")
    public R<ApCookbookInstruction> detail(@PathVariable("id") Long id) {
        return R.success(iApCookbookInstructionService.getById(id));
    }

    /**
     * 新增菜谱步骤
     */
    @Log(title = "新增菜谱步骤", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增菜谱步骤")
    @PostMapping("/add")
    public R<Void> add(@RequestBody ApCookbookInstructionRequest apCookbookInstructionRequest) {
        iApCookbookInstructionService.add(apCookbookInstructionRequest);
        return R.success();
    }

    /**
     * 修改菜谱步骤
     */
    @Log(title = "修改菜谱步骤", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改菜谱步骤")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody ApCookbookInstructionRequest apCookbookInstructionRequest) {
        iApCookbookInstructionService.update(apCookbookInstructionRequest);
        return R.success();
    }

    /**
     * 删除菜谱步骤
     */
    @Log(title = "删除菜谱步骤", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除菜谱步骤")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iApCookbookInstructionService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出菜谱步骤
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody ApCookbookInstructionRequest apCookbookInstructionRequest) throws IOException {
        List<ApCookbookInstruction> list = iApCookbookInstructionService.list(apCookbookInstructionRequest);
        ExcelUtils.exportExcel(list, ApCookbookInstruction.class, "菜谱步骤信息", response);
    }
}
